package com.xtecher.app.okhttp;

import com.zhy.http.okhttp.OkHttpUtils;

import java.io.File;

import okhttp3.MediaType;

/**
 * Created by luhao on 2016/12/21.
 */
public class OKHttpRequestManager implements IRequestManager {

    private static OKHttpRequestManager instance;

    public static OKHttpRequestManager getInstance() {
        if (instance == null) {
            synchronized (OKHttpRequestManager.class) {
                instance = new OKHttpRequestManager();
            }
        }
        return instance;
    }


    public void get(String url, OnLoadListener onLoadListener) {
        OkHttpUtils.get()
                .url(url)
                .build()
                .execute(new MyStringCallback(onLoadListener));
    }

    @Override
    public void postString(String url, String requestBody, OnLoadListener onLoadListener) {
        OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .url(url)
                .content(requestBody)
                .build()
                .execute(new MyStringCallback(onLoadListener));
    }

    @Override
    public void postWithToken(String url, String token, String requestBody, OnLoadListener onLoadListener) {
        OkHttpUtils.postString()
                .mediaType(MediaType.parse("application/json; charset=utf-8"))
                .addHeader("token", token)
                .url(url)
                .content(requestBody)
                .build()
                .execute(new MyStringCallback(onLoadListener));
    }

    @Override
    public void postFile(String url, String uid, File file, OnLoadListener onLoadListener) {
        OkHttpUtils.post()
                .url(url)
                .addParams("uid", uid)
                .addFile("file", file.getName(), file)
                .build()
                .execute(new MyStringCallback(onLoadListener));
    }
}
